package com.ssm.service.imp;

import com.github.pagehelper.PageHelper;
import com.ssm.mapper.CustomerMapper;
import com.ssm.mapper.OrderMapper;
import com.ssm.mapper.ProductMapper;
import com.ssm.mapper.ProductTypeMapper;
import com.ssm.pojo.Customer;
import com.ssm.pojo.Order;
import com.ssm.pojo.Product;
import com.ssm.service.OrderService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImp implements OrderService{

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ProductMapper productMapper;

    public void insert(Order order) {
        orderMapper.insert(order);
    }

    public void update(Order order) {
        orderMapper.update(order);
    }

    public void delete(String NO) {
        orderMapper.delete(NO);
    }

    public List<Order> selectOrdersPage(String orderNO, String customerNO, Short status, Integer pageNum, Integer pageSize) {
        if(pageNum !=null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        List<Order> orders = orderMapper.selectOrdersPage(orderNO,customerNO,status);
        if(orders!=null && orders.size()>0){
            for(Order order: orders){
                Customer c = customerMapper.selectByCustomerNO(order.getCustomerNO());
                order.setCustomer(c);
                Product p = productMapper.selectByNO(order.getProductNO());
                order.setProduct(p);
            }
        }
        return orders;
    }

    public Integer countSize(String orderNO,String customerNO, Short status) {
        Integer size = orderMapper.countSize(orderNO,customerNO,status);
        return size;
    }

    public Order selectByNO(String NO) {
        Order order = orderMapper.selectByNO(NO);
        if(order!=null){
            Customer c = customerMapper.selectByCustomerNO(order.getCustomerNO());
            order.setCustomer(c);
            Product p = productMapper.selectByNO(order.getProductNO());
            order.setProduct(p);
        }
        return order;
    }
}
